package com.springboot.mybatis.base.utils;

import com.springboot.mybatis.base.config.SpringContextHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

public class IdHelper {
    private static final Logger log = LoggerFactory.getLogger(IdHelper.class);
    public static final String PREFIX = "suixingpay.id.generator.";
    public static SnowFlake snowFlake = SnowFlake.builder().build();

    public IdHelper() {
    }

    public static final Long nextLongValue() {
        long instanceId = (Long)((Environment) SpringContextHelper.getBean(Environment.class)).getProperty("suixingpay.id.generator.instanceId", Long.class);
        Assert.notNull(instanceId, "ID生成器集群实例不允许为空！");
        Long identifier = (Long)((Environment)SpringContextHelper.getBean(Environment.class)).getProperty("suixingpay.id.generator.identifier", Long.class);
        Assert.notNull(identifier, "ID生成器业务标识不允许为空！");
        return snowFlake.setIdentifier(identifier).setInstanceId(instanceId).nextId();
    }

    public static final String nextStringValue() {
        return Long.toString(nextLongValue());
    }
}